package part1;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class OrderedLinkedList<E extends Comparable<? super E>> implements OrderedList<E> {
	private final LinkedList<E> l;
	
	public OrderedLinkedList() {
		l=new LinkedList<>();
	}
	
	public OrderedLinkedList(Iterator<E> it) {
		this();
		
		while (it.hasNext()) 
			add(it.next());
	}
	
	
	@Override
	public boolean contains(E e) {
		return ListUtil.contains(this, e);
	}

	@Override
	public E get(int index) {
		return l.get(index);
	}

	@Override
	public Iterator<E> iterator() {
		return l.iterator();
	}

	@Override
	public ListIterator<E> listIterator() {
		return l.listIterator();
	}

	@Override
	public ListIterator<E> listIterator(int size) {
		return l.listIterator(size);
	}

	@Override
	public boolean remove(E e) {
		return ListUtil.remove(this, e);
	}

	@Override
	public int size() {
		return l.size();
	}
	
	@Override
	public String toString() {
		return l.toString();
	}

	@Override
	public void reverse() {
		throw new UnsupportedOperationException("Cannot reverse an ordered list (with natural ordering)");
		
	}

	@Override
	public int compareTo(OrderedList<? extends E> o) {
		return ListUtil.compareTo(this, o);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((l == null) ? 0 : l.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof OrderedList<?>))
			return false;
		return ListUtil.equals(this, (OrderedList<?>) obj);
	}

	@Override
	public boolean add(E e) {
		return ListUtil.add(this, e);
	}

	@Override
	public boolean addAll(Iterable<E> o1) {
		return ListUtil.addAll(o1, this);
	}

	@Override
	public E max() {
		return ListUtil.max(this);
	}

	@Override
	public void merge(OrderedList<? extends E> list) {
		ListUtil.merge(list, this);
		
	}

}
